package com.zhong.gang.applib.utils;

import android.content.Context;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;

import com.zhong.gang.applib.R;

/**
 * Created by Administrator on 2016/12/21.
 */
public class CodeTimeTools extends CountDownTimer {
    private View mTextView;
    private Context context;
    private static int textColorIng = R.color.gray9;
    private static int textColorend = R.color.black;
    private static int bgColorend = R.color.black;
    private static int bgColorIng = R.color.black;
    public static void setTextColor(@ColorRes int ing, @ColorRes int end){
        textColorIng = ing;
        textColorend = end;
    }
    public static void setBackColor(@DrawableRes int bgColorend, @DrawableRes int bgColorIng){
        CodeTimeTools.bgColorIng = bgColorIng;
        CodeTimeTools.bgColorend = bgColorend;
    }


    public CodeTimeTools(Context context,View textView, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.mTextView = textView;
        this.context = context;
    }
    @Override
    public void onTick(long millisUntilFinished) {
        if (mTextView != null){
            mTextView.setClickable(false); //设置不可点击
            String str = millisUntilFinished / 1000 + "s再获取";
            if (mTextView instanceof Button) {
                ((Button) mTextView).setText(str);
                if (context != null) {
                    ((Button) mTextView).setTextColor(context.getResources().getColor(textColorIng));
                    ((TextView) mTextView).setBackgroundResource(bgColorIng);
                }
            }else if (mTextView instanceof TextView){
                ((TextView) mTextView).setText(str);
                if (context != null) {
                    ((TextView) mTextView).setTextColor(context.getResources().getColor(textColorIng));
                    ((TextView) mTextView).setBackgroundResource(bgColorIng);
                }
            }
        }
    }

    @Override
    public void onFinish() {
        if (mTextView != null) {
            if (mTextView instanceof Button) {
                ((Button) mTextView).setText("获取验证码");
                if (context != null) {
                    ((Button) mTextView).setTextColor(context.getResources().getColor(textColorend));
                    ((TextView) mTextView).setBackgroundResource(bgColorend);
                }
            }else if (mTextView instanceof TextView) {
                ((TextView) mTextView).setText("获取验证码");
                if (context != null) {
                    ((TextView) mTextView).setTextColor(context.getResources().getColor(textColorend));
                    ((TextView) mTextView).setBackgroundResource(bgColorend);
                }
            }
            mTextView.setClickable(true);//重新获得点击
        }
    }
}
